const state = {
  // 用户信息
  userInfo: {
    surname: "",
    name: "",
    gender: "",
    year: 0,
    month: 0,
    day: 0,
    hour: 0,
    minute: 0,
    sourceForm: "test",

    userName: "",
    birthText: "",
  },
  // 结果信息
  otherUserInfo: {
    celestialItems: [
      { label: "年", value: "" },
      { label: "月", value: "" },
      { label: "日", value: "" },
      { label: "时", value: "" },
    ],
    shishenCard: [
      {
        label: "年柱",
        content: "",
      },
      {
        label: "月柱",
        content: "",
      },
      {
        label: "日柱",
        content: "",
      },
      {
        label: "时柱",
        content: "",
      },
    ],

    birthChart: {
      headers: ["四柱", "年柱", "月柱", "日柱", "时柱"],
      subHeaders: ["天干+地支", "", "", "", ""],
    },
    patternAnalysis: {
      description:
        "古代道家哲学系统将万物分为：水(代表润下)、火(代表炎上)、金(代表收敛)、木(代表伸展)、土(代表中和)五种元素。",
      headers: ["元素", "木", "火", "土", "金", "水"],
      data: ["强度", "", "", "", "", ""],
    },
    basicInfo: {
      headers: ["姓氏", "性别", "出生日期"],
    },
  },

  //婚姻测算
  marriageResultInfo: [
    {
      name: "综合报告",
      content: [],
    },
    {
      name: "配偶分析",
      content: [],
    },
    {
      name: "恋爱分析",
      content: [],
    },
    {
      name: "婚姻状况",
      content: [],
    },
  ],
  //其他测算
  otherResultInfo: [
    {
      name: "财运/性格",
      content: [],
    },
    {
      name: "家庭",
      content: [],
    },
    {
      name: "健康",
      content: [],
    },
    {
      name: "事业",
      content: [],
    },
   
  ],
  marryPaid: false,   // 姻缘类支付
  otherPaid: false,   // 其他类支付
  hehunPaid: false    // 合婚类支付
};

const mutations = {
  SET_USER_INFO(state, data) {
    state.userInfo = { ...state.userInfo, ...data };
  },
  SET_OTHER_USER_INFO(state, data) {
    // 拼装数据
    const assembledData = {
      ...state.otherUserInfo,
      ...data,
      celestialItems: [
        { label: "年", value: data.baZiInfo?.yearZhu || "" },
        { label: "月", value: data.baZiInfo?.monthZhu || "" },
        { label: "日", value: data.baZiInfo?.dayZhu || "" },
        { label: "时", value: data.baZiInfo?.hourZhu || "" },
      ],
      shishenCard: [
        {
          label: "主管姻缘十神",
          value: data.hunyinInfo?.hyShiShen || "",
        },
        {
          label: "夫妻星位置",
          value: data.hunyinInfo?.fqxLocation || "",
        },
        {
          label: "日支十神",
          value: data.hunyinInfo?.dayZhiShiShen || "",
        },
        {
          label: "日柱透干",
          value: data.hunyinInfo?.touGan,
        },
      ],
      basicInfo: {
        headers: ["姓氏", "性别", "出生日期"],
      },
      birthChart: {
        headers: ["四柱", "年柱", "月柱", "日柱", "时柱"],
        subHeaders: [
          "天干+地支",
          data.baZiInfo?.yearZhu || "",
          data.baZiInfo?.monthZhu || "",
          data.baZiInfo?.dayZhu || "",
          data.baZiInfo?.hourZhu || "",
        ],
      },
      patternAnalysis: {
        description:
          "古代道家哲学系统将万物分为：水(代表润下)、火(代表炎上)、金(代表收敛)、木(代表伸展)、土(代表中和)五种元素。",
        headers: ["元素", "木", "火", "土", "金", "水"],
        data: [
          "强度",
          data.wuXingInfo?.woodStatus || "",
          data.wuXingInfo?.fireStatus || "",
          data.wuXingInfo?.earthStatus || "",
          data.wuXingInfo?.metalStatus || "",
          data.wuXingInfo?.waterStatus || "",
        ],
      },
    };
    state.otherUserInfo = assembledData;
  },
  SET_MARRIAGE_RESULT_INFO(state, data) {
    const allContent = [];

    data.forEach((item) => {
      if (item.content && Array.isArray(item.content)) {
        allContent.push(...item.content);
      }
    });

    state.marriageResultInfo = {
      result: data,
      featureList: allContent,
    };
  },
  SET_OTHER_RESULT_INFO(state, data) {
    // 定义期望的排序顺序
    const orderMap = {
      "财运/性格": 0,
      "家庭": 1,
      "健康": 2,
      "事业": 3
    };
    
    // 按照指定顺序排序
    const sortedData = data.sort((a, b) => {
      const orderA = orderMap[a.name] !== undefined ? orderMap[a.name] : 999;
      const orderB = orderMap[b.name] !== undefined ? orderMap[b.name] : 999;
      return orderA - orderB;
    });
    
    state.otherResultInfo = sortedData;
  },
  SET_MARRY_PAID(state, val) {
    state.marryPaid = val;
  },
  SET_OTHER_PAID(state, val) {
    state.otherPaid = val;
  },
  SET_HEHUN_PAID(state, val) {
    state.hehunPaid = val;
  }
};

const actions = {
  setUserInfo({ commit }, userInfo) {
    commit("SET_USER_INFO", userInfo);
    return Promise.resolve();
  },
  setOtherUserInfo({ commit }, otherUserInfo) {
    commit("SET_OTHER_USER_INFO", otherUserInfo);
    return Promise.resolve();
  },
  setMarriageInfo({ commit }, marriageResultInfo) {
    commit("SET_MARRIAGE_RESULT_INFO", marriageResultInfo);
    return Promise.resolve();
  },
  setOtherResultInfo({ commit }, otherResultInfo) {
    commit("SET_OTHER_RESULT_INFO", otherResultInfo);
    return Promise.resolve();
  },
};

const getters = {
  userInfo: (state) => state.userInfo,
  otherUserInfo: (state) => state.otherUserInfo,
  marriageResultInfo: (state) => state.marriageResultInfo,
  otherResultInfo: (state) => state.otherResultInfo,
  marryPaid: state => state.marryPaid,
  otherPaid: state => state.otherPaid,
  hehunPaid: state => state.hehunPaid
};

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions,
};
